sap.ui.define([
    "sap/ui/core/mvc/Controller",
    'sap/ui/comp/smartvariants/PersonalizableInfo',
    'sap/ui/model/Filter',
    'sap/ui/model/FilterOperator'
],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, PersonalizableInfo, Filter, FilterOperator) {
        "use strict";

        return Controller.extend("ikdproject310.controller.Main", {
            onInit: function () {
                this.oSmartVariantManagement = this.getView().byId("svm");
                this.oExpandedLabel = this.getView().byId("expandedLabel");
                this.oSnappedLabel = this.getView().byId("snappedLabel");
                this.oFilterBar = this.getView().byId("filterbar");
                this.oTable = this.getView().byId("MainTable");

                this.oFilterBar.registerGetFiltersWithValues(this.getFiltersWithValues);
                var oPersInfo = new PersonalizableInfo({
                    type: "filterBar",
                    keyName: "persistencyKey",
                    dataSource: "",
                    control: this.oFilterBar
                });
                this.oSmartVariantManagement.addPersonalizableControl(oPersInfo);
                this.oSmartVariantManagement.initialise(function () { }, this.oFilterBar);



                var oRouter = this.getOwnerComponent().getRouter();

                try {
                    var param = this.getOwnerComponent().getComponentData().startupParameters;

                    if (Object.keys(param).length > 0) {
                        var ctno = 'CTNO_' + param.Cargmtno[0]
                        oRouter.navTo("RouteDetail", { Uuid: ctno });
                    }
                } catch (error) {

                }


                // var ctno = 'CTNO_' + '21EASK14G1I60490001'

                // this.getView().getModel().callFunction("/call_api",   //call_api는 behavior에서 정의한 액션 
                //     {
                //         method: "POST", // 무조건 POST 사용 
                //         urlParameters: {   //정의한 파라미터 , 무조건 첫문자만 대문자로 써야됨 
                //             "Cargmtno": '21EASK14G1I60490001',
                //             "Mblno": "A"
                //         },
                //         success: function (odata, response) {
                //             //Model Refresh
                //             that.getView().getModel().refresh(true);
                //             // oGlobalBusyDialog.close();

                //             // var uuid = oEvent.getSource().getBindingContext().getProperty("Uuid");

                //             // console.log("List sOrderId:========" + uuid)
                //             // var oRouter = that.getOwnerComponent().getRouter();
                //             // // console.log(oEvent.getSource().getBindingContext().sPath)
                //             // // var sPath = oEvent.getSource().getBindingContext().sPath;
                //             // // console.log("sPath-------------",sPath)
                //             // oRouter.navTo("RouteDetail", { Uuid: uuid });
                //         },
                //         error: function (res) {
                //             console.log(res)
                //         }
                //     })

            },
            onExit: function () {
                this.oModel = null;
                this.oSmartVariantManagement = null;
                this.oExpandedLabel = null;
                this.oSnappedLabel = null;
                this.oFilterBar = null;
                this.oTable = null;
            },
            getFiltersWithValues: function () {

                var aFiltersWithValue = this.oFilterBar.getFilterGroupItems().reduce(function (aResult, oFilterGroupItem) {
                    var oControl = oFilterGroupItem.getControl();

                    if (oControl && oControl.getSelectedKeys && oControl.getSelectedKeys().length > 0) {
                        aResult.push(oFilterGroupItem);
                    }

                    return aResult;
                }, []);


                return aFiltersWithValue;
            },
            onSelectionChange: function (oEvent) {
                // debugger
                this.oSmartVariantManagement.currentVariantSetModified(true);
                this.oFilterBar.fireFilterChange(oEvent);
            },
            onPressItem: function (oEvent) {
                // var oGlobalBusyDialog = new sap.m.BusyDialog();
                // oGlobalBusyDialog.open();

                var uuid = oEvent.getSource().getBindingContext().getProperty("Uuid");

                console.log("List sOrderId:========" + uuid)
                var oRouter = this.getOwnerComponent().getRouter();
                // console.log(oEvent.getSource().getBindingContext().sPath)
                // var sPath = oEvent.getSource().getBindingContext().sPath;
                // console.log("sPath-------------",sPath)
                oRouter.navTo("RouteDetail", { Uuid: uuid });
                // oRouter.navTo("SalesOrderDetail1", { orderId: sPath });
                // oRouter.navTo("SalesOrderDetail1", { orderId: window.encodeURIComponent(sPath) });
            },
            onClear: function () {
                // debugger
                // this.byId("sfield").setValue("")
                // this.byId("FilterSalesOrder").setValue("")
                // // debugger
                // this.byId("FilterSalesOrderStatus").setSelectedKeys(null)
                // // this.byId("CustomerVH").destroyTokens()
                // this.byId("CustomerVH").setTokens([])
                // this.oTable.getBinding("items").filter([]);
            },
            onSearch: function (oEvent) {
                // alert('ddd')
                var that = this
                var filterCargmtno = this.byId("FilterCargmtno").getValue()
                // debugger
                // alert(filterCargmtno)
                if (filterCargmtno == '') {
                    filterCargmtno = '21EASK14G1I60490001'
                }

                var oGlobalBusyDialog = new sap.m.BusyDialog();
                oGlobalBusyDialog.open();

                this.getView().getModel().callFunction("/call_api",   //call_api는 behavior에서 정의한 액션 
                    {
                        method: "POST", // 무조건 POST 사용 
                        urlParameters: {   //정의한 파라미터 , 무조건 첫문자만 대문자로 써야됨 
                            "Cargmtno": filterCargmtno,
                            "Mblno": "A"
                        },
                        success: function (odata, response) {
                            //Model Refresh
                            that.getView().getModel().refresh(true);
                            oGlobalBusyDialog.close();
                        },
                        error: function (res) {
                            console.log(res)
                        }
                    })
                // debugger
                // console.log(this)
                // var aTableFilters = this.oFilterBar.getFilterGroupItems().reduce(function (aResult, oFilterGroupItem) {
                //     var oControl = oFilterGroupItem.getControl();
                //     var aValue
                //     var aTokens
                //     var aSelectedKeys
                //     var aFilters = [];
                //     // debugger

                //     if (oFilterGroupItem.getName() == "Sfield") {
                //         aValue = oControl.getValue();

                //         if (aValue) {
                //             aFilters.push(new Filter({
                //                 path: "Kunnr",
                //                 operator: FilterOperator.Contains,
                //                 value1: aValue
                //             }));
                //             aFilters.push(new Filter({
                //                 path: "Vbeln",
                //                 operator: FilterOperator.Contains,
                //                 value1: aValue
                //             }));
                //             aFilters.push(new Filter({
                //                 path: "Vkorg",
                //                 operator: FilterOperator.Contains,
                //                 value1: aValue
                //             }));
                //             aResult.push(new Filter({
                //                 filters: aFilters,
                //                 and: false
                //             }));
                //         }

                //         return aResult;
                //     }

                //     if (oControl instanceof sap.m.MultiInput) {
                //         // 
                //         aTokens = oControl.getTokens();

                //         aFilters = aTokens.map(function (oToken) {
                //             var oRange = oToken.data("range");
                //             // debugger
                //             if (oRange) {
                //                 return new Filter({
                //                     path: oFilterGroupItem.getName(),
                //                     // exclude: oToken.data().range.exclude,
                //                     operator: oRange.exclude ? "NE" : oRange.operation,
                //                     value1: oRange.value1,
                //                     value2: oRange.value2
                //                 });
                //             } else {
                //                 return new Filter({
                //                     path: oFilterGroupItem.getName(),
                //                     operator: "EQ",
                //                     value1: oToken.getKey() == "" ? oToken.getText() : oToken.getKey()
                //                 });
                //             }

                //         });
                //         // debugger
                //         if (aTokens.length > 0) {
                //             aResult.push(new Filter({
                //                 filters: aFilters,
                //                 and: false
                //             }));
                //         }

                //     } else if (oControl instanceof sap.m.Input) {
                //         // debugger
                //         aValue = oControl.getValue();
                //         aFilters.push(new Filter({
                //             path: oFilterGroupItem.getName(),
                //             operator: FilterOperator.Contains,
                //             value1: aValue
                //         }));
                //         if (aValue) {

                //             aResult.push(new Filter({
                //                 filters: aFilters,
                //                 and: false
                //             }));
                //         }
                //     } else if (oControl instanceof sap.m.MultiComboBox) {
                //         // debugger
                //         aSelectedKeys = oControl.getSelectedKeys();

                //         aFilters = aSelectedKeys.map(function (sSelectedKey) {
                //             return new Filter({
                //                 path: oFilterGroupItem.getName(),
                //                 operator: FilterOperator.Contains,
                //                 value1: sSelectedKey
                //             });
                //         });
                //         if (aSelectedKeys.length > 0) {
                //             aResult.push(new Filter({
                //                 filters: aFilters,
                //                 and: false
                //             }));
                //         }
                //     }

                //     return aResult;
                // }, []);

                // this.oTable.getBinding("items").filter(aTableFilters);
                // this.oTable.setShowOverlay(false);
            },
        });
    });
